package com.hrt.datasender.datacollect.serial.protocol;

import java.util.ArrayList;
import java.util.List;

import com.hrt.datasender.mq.model.SerialModel;
import com.hrt.serial.modbus.ModbusDataDecode;
import com.hrt.serial.modbus.ModbusDataDecodeRTU;

public class ModbusRTUProtocol implements ISerialDataProtocol {

    private ModbusDataDecode modbusDataDecode = new ModbusDataDecodeRTU();

    @Override
    public String getProtocolName() {
        return "Modbus协议";
    }

    @Override
    public List<SerialModel> decode(byte[] data, int datalength, String comNum) {
        List<byte[]> datas = modbusDataDecode.dataFrameDecode(data, datalength);
        List<SerialModel> models = new ArrayList<>();

        for (byte[] dataItem : datas) {
            SerialModel model = new SerialModel(dataItem, comNum, dataItem[0] + "");
            models.add(model);
        }

        return models;
    }

}
